// Fill out your copyright notice in the Description page of Project Settings.


#include "SPakPlatformsView.h"
#include "SlateOptMacros.h"
#include "Widgets/SBoxPanel.h"
#include "SPakPlatformRow.h"
#include "HotPakerFileLib.h"
#include "Interfaces/ITargetPlatformManagerModule.h"
#include "Interfaces/ITargetPlatform.h"


#define LOCTEXT_NAMESPACE "SPakPlatformsView"

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SPakPlatformsView::Construct(const FArguments& InArgs)
{

	TArray<ITargetPlatform*> AllPlatforms = GetTargetPlatformManager()->GetTargetPlatforms();
	if (AllPlatforms.Num() > 0)
	{
		PlatformList.Reset();
		for (auto Platform : AllPlatforms)
			PlatformList.Add(MakeShareable(new FString(Platform->PlatformName())));
	}

	ChildSlot
		[
			SNew(SVerticalBox)
			+ SVerticalBox::Slot()
		.AutoHeight()
		.MaxHeight(256.0f)
		[
			SAssignNew(PlatformsView, SListView<TSharedPtr<FString>>)
			.HeaderRow(
				SNew(SHeaderRow)
				.Visibility(EVisibility::Collapsed)

				+ SHeaderRow::Column("PlatformName")
				.DefaultLabel(LOCTEXT("PlatformNameColumnHeader", "Platform"))
				.FillWidth(1.0f)
			)
		.ItemHeight(16.0f)
		.ListItemsSource(&PlatformList)
		.OnGenerateRow(this, &SPakPlatformsView::HandlePlatformViewGenerateRow)
		.SelectionMode(ESelectionMode::None)
		]
		];

}

void SPakPlatformsView::AddSelectPlatform(const FString& InPlatform)
{
	if (!SelectPlatforms.Contains(InPlatform))
		SelectPlatforms.Add(InPlatform);
}

void SPakPlatformsView::RemoveSelectPlatform(const FString& InPlatform)
{
	if (SelectPlatforms.Contains(InPlatform))
		SelectPlatforms.Remove(InPlatform);
}

const TArray<FString>& SPakPlatformsView::GetAllSelectPlatforms()
{
	return SelectPlatforms;
}

void SPakPlatformsView::ClearAllSelectPlatforms()
{
	if (SelectPlatforms.Num() > 0)
		SelectPlatforms.Reset();
}

END_SLATE_FUNCTION_BUILD_OPTIMIZATION

TSharedRef<ITableRow> SPakPlatformsView::HandlePlatformViewGenerateRow(TSharedPtr<FString> PlatformName, const TSharedRef<STableViewBase>& OwnerTable)
{
	return SNew(SPakPlatformRow, this)
		.PlatformName(PlatformName)
		.OwnerTableView(OwnerTable);
}


#undef LOCTEXT_NAMESPACE
